#include "lock_impl_win32.h"

sys::win32_lock_cs::win32_lock_cs()
{
	InitializeCriticalSection(&m_cs);
}

sys::win32_lock_cs::~win32_lock_cs()
{
	if (is_locked())
	{
		unlock();
	}
	DeleteCriticalSection(&m_cs);
}

cmn::dword sys::win32_lock_cs::release()
{
	delete this;
	return 0;
}

bool sys::win32_lock_cs::do_lock()
{
	EnterCriticalSection(&m_cs);
	return true;
}

bool sys::win32_lock_cs::try_lock()
{
	if (TRUE == TryEnterCriticalSection(&m_cs))
	{
		return true;
	}
	return false;
}

bool sys::win32_lock_cs::is_locked()
{
	if (try_lock())
	{
		unlock();
		return true;
	}
	return false;
}

void sys::win32_lock_cs::unlock()
{
	LeaveCriticalSection(&m_cs);
}
